package com.example.vp2loadmoreview.banner;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.vp2loadmoreview.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

public class Vp2Adapter extends RecyclerView.Adapter<Vp2Adapter.Vp2ViewHolder> {
    private Context mContext;
    private List<String> mList;

    private int[] colors = new int[]{0xff2196f3,0xfff44336,0xff9c27b0,0xff2196f3,0xfff44336};
    public Vp2Adapter(Context mContext,List<String> list){
        this.mContext = mContext;
        mList = new ArrayList<>();
        mList.addAll(list);
        mList.add(0,mList.get(mList.size() - 1));
        mList.add(mList.get(0));
    }

    @NonNull
    @Override
    public Vp2ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new Vp2ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.vp2_item,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull Vp2ViewHolder vp2ViewHolder, int i) {
        vp2ViewHolder.tv.setText(mList.get(i % colors.length));
        vp2ViewHolder.bgView.setBackgroundColor(colors[i % colors.length]);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class Vp2ViewHolder extends RecyclerView.ViewHolder {
        private ConstraintLayout bgView;
        private TextView tv;

        public Vp2ViewHolder(@NonNull View itemView) {
            super(itemView);
            bgView = itemView.findViewById(R.id.bgView);
            tv = itemView.findViewById(R.id.tv);
        }
    }
}
